package webServer

import (
	"7qule.com/sd3/CrossServer/src/utils"
	"fmt"
	"net/http"
	"sync"
	//"log"
	"net/http/pprof"

	"7qule.com/sd3/CrossServer/src/config"
	"public.com/Framework/goroutineMgr"
	"public.com/goutil/logUtil"
)

// 启动服务器
// wg：WaitGroup对象
func Start(wg *sync.WaitGroup) {
	// 处理goroutine数量
	goroutineName := "webServer.Start"
	goroutineMgr.Monitor(goroutineName)
	defer goroutineMgr.ReleaseMonitor(goroutineName)
	defer func() {
		utils.LogErrorRecover()
		wg.Done()
	}()

	// 启动过程中不需要捕获异常
	logUtil.LogAndPrint(fmt.Sprintf("Web服务器开始监听:%v", config.WerbServerAddress), logUtil.Info)

	// 启动Web服务器监听
	mux := http.NewServeMux()
	mux.Handle("/", &selfDefineMux{})
	mux.HandleFunc("/debug/pprof/", pprof.Index)
	mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
	mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
	mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
	mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
	err := http.ListenAndServe(config.WerbServerAddress, mux)
	if err != nil {
		panic(fmt.Errorf("ListenAndServe失败，错误信息为：%s", err))
	}
}
